<?php

$ws = new swoole_websocket_server('172.17.0.2',9502);



$ws->set(['task_worker_num'=>4]);

$ws->on('WorkerStart', function ($ws, $worker_id) { 
   	echo 1;
});


$ws->on('connect',function($ws,$id){
	echo 2;
	//状态 
	print_r($ws->stats());
});

//监听WebSocket连接打开事件 (这里会得到很多信息)
$ws->on('open',function($ws,$request){
	echo 3;
});
$ws->on("message",function($ws,$frame){
	echo 4;
	$task_id = $ws->task($data);
    echo "异步任务投递成功: id=$task_id\n";
    print_r($frame);
    $ws->push($frame->fd, "数据已接收，处理中...");
	// $ws->task($frame);
	
});

//处理异步任务
$ws->on('task', function ($ws, $task_id, $from_id, $data) {
	echo 6;
	
   	echo "新的待处理异步任务[id=$task_id]".PHP_EOL;
    // todo 处理异步任务
    // 返回任务执行的结果
    $ws->finish("OK");

});

//处理异步任务的结果
$ws->on('finish', function ($ws, $task_id, $data) {
	echo 7;
    echo "[$task_id] Finish:".$data;
});



$ws->on('close', function ($ws, $fd) {
    echo "client-{$fd} is closed\n";
});

$ws->start();